package com.itzyf.pokemondata.activity.gallery

import android.content.Intent
import android.text.TextUtils
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.bean.DbComprehensiveDo
import com.itzyf.pokemondata.utils.OnRefreshListener
import com.itzyf.pokemondata.utils.setOnRefreshListener
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_list.*
import org.litepal.LitePal


@Deprecated("弃用")
class GalleryActivity : BaseActivity() {
    var mDatas: MutableList<GalleryDo?> = ArrayList()
    private lateinit var mAdapter: QuickAdapter
    var index = 0

    override fun handlerActivity() {
        setTitle(intent.getStringExtra("title"))
        mAdapter = QuickAdapter(R.layout.item_text, mDatas)
        rv_list.adapter = mAdapter

        rv_list.layoutManager = LinearLayoutManager(this)
        mAdapter.setOnLoadMoreListener({ queryDatas(index++) }, rv_list)

        pull_to_refresh.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh() {
                index = 0
                queryDatas(index)
            }
        })
        mSearchView.hint = "请输入名称"
        mBtnSearch.setOnClickListener {
            val query = mSearchView.text.toString()
            queryDatas(query = query)
        }
        mSearchView.setOnEditorActionListener(TextView.OnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                val query = mSearchView.text.toString()
                queryDatas(query = query)
                return@OnEditorActionListener true
            }
            false
        })
        mAdapter.setOnItemClickListener { _, _, position ->
            if (mDatas[position]!!.type == 0) {
                val intent = Intent(this@GalleryActivity, ImageZoomActivity::class.java)
                intent.putExtra("image", mDatas[position]!!.image)
                intent.putExtra("title", mDatas[position]!!.title)
                startActivity(intent)
            } else if (mDatas[position]!!.type == 1) {
                val intent = Intent(this@GalleryActivity, MarkdownActivity::class.java)
                intent.putExtra("objId", mDatas[position]!!.objectId)
                intent.putExtra("title", mDatas[position]!!.title)
                startActivity(intent)
            } else if (mDatas[position]!!.type == 2) {
                val intent = Intent(this@GalleryActivity, WebViewActivity::class.java)
                intent.putExtra("httpUrl", mDatas[position]!!.httpUrl)
                intent.putExtra("title", mDatas[position]!!.title)
                startActivity(intent)
            }
        }
        queryDatas()
    }

    /**
     * 查询数据
     */
    fun queryDatas(index: Int = 0, pageSize: Int = 20, query: String = "") {
        var where = ""
        if (!TextUtils.isEmpty(query)) {
            where = "and name like '%$query%'"
        }
        val dataList =
            LitePal
                .select("name", "imageUrl", "type", "httpUrl","objectId")
                .limit(pageSize)
                .offset(index * pageSize)
                .order("createdAt desc")
                .where("cTypeId = ? $where", intent.getStringExtra("objTypeId"))
                .find(DbComprehensiveDo::class.java)

        if (index == 0) {
            mDatas.clear()
            pull_to_refresh.finishRefresh()
        }

        for (obj in dataList) {
            mDatas.add(
                GalleryDo(
                    obj.name,
                    obj.imageUrl,
                    obj.httpUrl,
                    obj.type,
                    obj.objectId
                )
            )
        }

        if (dataList.size < pageSize) {
            mAdapter.loadMoreEnd()
        } else {
            mAdapter.loadMoreComplete()
        }
        mAdapter.notifyDataSetChanged()
    }

    internal inner class QuickAdapter(layoutResId: Int, data: List<GalleryDo?>) :
        BaseQuickAdapter<GalleryDo, BaseViewHolder>(layoutResId, data) {

        override fun convert(helper: BaseViewHolder, item: GalleryDo?) {
            helper.setText(R.id.text, item?.title)
        }
    }


    override fun getContentViewId() = R.layout.activity_list

    override fun getToolbarTitle() = "综合大全"

    data class GalleryDo(
        val title: String,
        val image: String?,
        val httpUrl: String?,
        val type: Int,
        val objectId: String
    )
}

